﻿using System;
using System.Web.Mvc;
using Utilities;

namespace Website.Infrastructure
{
    public static class SwitchLanguageHelper
    {
        public static MvcHtmlString LanguageSwitch(this HtmlHelper helper, string currDomain, string imageUrl, string altText, string newDomain)
        {
            var url = helper.ViewContext.HttpContext.Request.Url;

            if (!url.Host.ToLower().EndsWith(currDomain))
            {
                return MvcHtmlString.Empty;
            }

            url = url.ReplaceTopLevelDomain(currDomain, newDomain);

            var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);

            var hyperlink = CreateHyperlinkTag(url);
            var imgLink = CreateImageTag(urlHelper.Content(imageUrl), altText);
            hyperlink.InnerHtml = imgLink.ToString(TagRenderMode.SelfClosing);

            return new MvcHtmlString(hyperlink.ToString());
        }

        private static TagBuilder CreateHyperlinkTag(Uri url)
        {
            var builder = new TagBuilder("a");

            builder.MergeAttribute("href", url.ToString());

            return builder;
        }

        public static TagBuilder CreateImageTag(string url, string altText)
        {
            var builder = new TagBuilder("img");

            builder.MergeAttribute("src", url);
            builder.MergeAttribute("alt", altText);

            return builder;
        }
    }
}